<template>
  <el-form :model="form" label-width="120px">
    <el-form-item label="账号">
      <el-input v-model="form.username" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="form.password" type="password" />
    </el-form-item>
    <el-form-item label="角色">
      <el-select v-model="roleIdx" class="m-2" placeholder="请选择账号的角色">
        <el-option
          v-for="(item, index) in roleList"
          :key="item.objectId"
          :label="item.roleName"
          :value="index"
        />
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">新增账号</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { userReg, roleGet } from "../../api/user";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      roleIdx: "",
      roleList: [],
    };
  },
  created() {
    roleGet().then((res) => {
      this.roleList = res.data.results;
    });
  },
  methods: {
    onSubmit() {
      // console.log(this.form);
      let { objectId: roleId, roleName } = this.roleList[this.roleIdx];
      // console.log(roleId);
      userReg({ ...this.form, roleId, roleName });
    },
  },
};
</script>
